#include "calculator.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QDebug>

#define BTN_HEIGHT 90
#define BTN_WEIGHT 90

Calculator::Calculator(QWidget *parent)
    : QWidget(parent)
{
    double result = 0; // 记录每次点击运算符之前的运算结果
    QString oper = ""; // 记录上一个输入的运算符

    this->setWindowTitle("计算器");
    this->setFixedSize(360, 540);

    // 显示窗口
    QLineEdit* showEdit = new QLineEdit(this);
    showEdit->resize(360, BTN_HEIGHT);
    showEdit->setFont(QFont("微软雅黑", 14));

    // 清除按钮
    QPushButton* clearBut = new QPushButton("C", this);
    clearBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    clearBut->move(0, BTN_WEIGHT);
    connect(clearBut, &QPushButton::clicked, showEdit, &QLineEdit::clear);

    // *
    QPushButton* multBut = new QPushButton("*", this);
    multBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    multBut->move(90, 90);
    connect(multBut, &QPushButton::clicked, [multBut, showEdit](){
        QString prev_text = showEdit->text();
        if(prev_text == "")
        {
            return;
        }
        prev_text.push_back(multBut->text());
        showEdit->setText(prev_text);
    });

    // ÷
    QPushButton* devBut = new QPushButton("÷", this);
    devBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    devBut->move(180, 90);
    connect(devBut, &QPushButton::clicked, [devBut, showEdit](){
        QString prev_text = showEdit->text();
        if(prev_text == "")
        {
            return;
        }
        prev_text.push_back(devBut->text());
        showEdit->setText(prev_text);
    });

    // back
    QPushButton* backBut = new QPushButton("back", this);
    backBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    backBut->move(270, 90);
    connect(backBut, &QPushButton::clicked, [showEdit](){
        QString prev_text = showEdit->text();
        QString new_text;
        for (int i = 0;i < prev_text.size() - 1;i++)
        {
            new_text.push_back(prev_text[i]);
        }
        showEdit->setText(new_text);

        // prev_text.chop(1); // 移除最后一个字符
        // showEdit->setText(prev_text);
    });

    // 7
    QPushButton* sevenBut = new QPushButton("7", this);
    sevenBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    sevenBut->move(0, 180);
    connect(sevenBut, &QPushButton::clicked, [sevenBut, showEdit](){
        QString prev_text = showEdit->text();
        prev_text.push_back(sevenBut->text());
        showEdit->setText(prev_text);
    });

    // 8
    QPushButton* eightBut = new QPushButton("8", this);
    eightBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    eightBut->move(90, 180);
    connect(eightBut, &QPushButton::clicked, [eightBut, showEdit](){
        QString prev_text = showEdit->text();
        prev_text.push_back(eightBut->text());
        showEdit->setText(prev_text);
    });

    // 9
    QPushButton* nineBut = new QPushButton("9", this);
    nineBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    nineBut->move(180, 180);
    connect(nineBut, &QPushButton::clicked, [nineBut, showEdit](){
        QString prev_text = showEdit->text();
        prev_text.push_back(nineBut->text());
        showEdit->setText(prev_text);
    });

    // +
    QPushButton* addBut = new QPushButton("+", this);
    addBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    addBut->move(270, 180);
    connect(addBut, &QPushButton::clicked, [addBut, showEdit](){
        QString prev_text = showEdit->text();
        if(prev_text == "")
        {
            return;
        }
        prev_text.push_back(addBut->text());
        showEdit->setText(prev_text);
    });

    // 4
    QPushButton* fourBut = new QPushButton("4", this);
    fourBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    fourBut->move(0, 270);
    connect(fourBut, &QPushButton::clicked, [fourBut, showEdit](){
        QString prev_text = showEdit->text();
        prev_text.push_back(fourBut->text());
        showEdit->setText(prev_text);
    });


    // 5
    QPushButton* fiveBut = new QPushButton("5", this);
    fiveBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    fiveBut->move(90, 270);
    connect(fiveBut, &QPushButton::clicked, [fiveBut, showEdit](){
        QString prev_text = showEdit->text();
        prev_text.push_back(fiveBut->text());
        showEdit->setText(prev_text);
    });


    // 6
    QPushButton* sixBut = new QPushButton("6", this);
    sixBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    sixBut->move(180, 270);
    connect(sixBut, &QPushButton::clicked, [sixBut, showEdit](){
        QString prev_text = showEdit->text();
        prev_text.push_back(sixBut->text());
        showEdit->setText(prev_text);
    });

    // -
    QPushButton* subBut = new QPushButton("-", this);
    subBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    subBut->move(270, 270);
    connect(subBut, &QPushButton::clicked, [subBut, showEdit](){
        QString prev_text = showEdit->text();
        if(prev_text == "")
        {
            return;
        }
        prev_text.push_back(subBut->text());
        showEdit->setText(prev_text);
    });

    // 1
    QPushButton* oneBut = new QPushButton("1", this);
    oneBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    oneBut->move(0, 360);
    connect(oneBut, &QPushButton::clicked, [oneBut, showEdit](){
        QString prev_text = showEdit->text();
        prev_text.push_back(oneBut->text());
        showEdit->setText(prev_text);
    });

    // 2
    QPushButton* twoBut = new QPushButton("2", this);
    twoBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    twoBut->move(90, 360);
    connect(twoBut, &QPushButton::clicked, [twoBut, showEdit](){
        QString prev_text = showEdit->text();
        prev_text.push_back(twoBut->text());
        showEdit->setText(prev_text);
    });

    // 3
    QPushButton* threeBut = new QPushButton("3", this);
    threeBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    threeBut->move(180, 360);
    connect(threeBut, &QPushButton::clicked, [threeBut, showEdit](){
        QString prev_text = showEdit->text();
        prev_text.push_back(threeBut->text());
        showEdit->setText(prev_text);
    });

    // =
    QPushButton* equalBut = new QPushButton("=", this);
    equalBut->resize(BTN_WEIGHT, BTN_HEIGHT * 2);
    equalBut->move(270, 360);

    // %
    QPushButton* remBut = new QPushButton("%", this);
    remBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    remBut->move(0, 450);
    connect(remBut, &QPushButton::clicked, [remBut, showEdit](){
        QString prev_text = showEdit->text();
        if(prev_text == "")
        {
            return;
        }
        prev_text.push_back(remBut->text());
        showEdit->setText(prev_text);
    });

    // 0
    QPushButton* zeroBut = new QPushButton("0", this);
    zeroBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    zeroBut->move(90, 450);
    connect(zeroBut, &QPushButton::clicked, [zeroBut, showEdit](){
        QString prev_text = showEdit->text();
        if(prev_text == "")
        {
            return;
        }
        prev_text.push_back(zeroBut->text());
        showEdit->setText(prev_text);
    });

    // .
    QPushButton* pointBut = new QPushButton(".", this);
    pointBut->resize(BTN_WEIGHT, BTN_HEIGHT);
    pointBut->move(180, 450);
    connect(pointBut, &QPushButton::clicked, [pointBut, showEdit](){
        QString prev_text = showEdit->text();
        if(prev_text == "")
        {
            return;
        }
        prev_text.push_back(pointBut->text());
        showEdit->setText(prev_text);
    });


}


Calculator::~Calculator()
{

}
